<?php

namespace Biz\service\BrandRights;

use Biz\model\brandRights\BrandLevelRightsSource as mBrandLevelRightsSource;
use Biz\model\brandRights\BrandRightsSource as mBrandRightsSource;

class BrandLevelSourceRightsService
{

    public static function getLevelSourceRights()
    {
        $where['ORDER'] = ['sort_weight' => 'ASC'];
        $levelRights = mBrandLevelRightsSource::getBrandLevelSourceRights(mBrandLevelRightsSource::STATUS_NORMAL,
            $where);

        $levelRightsIds = empty($levelRights) ? [] : array_column($levelRights, 'source_rights_id');

        $levelSourceRights = [];

        if (empty($levelRightsIds)) {
            return $levelSourceRights;
        }

        $sWhere['id'] = $levelRightsIds;

        $sWhere['data_status'] = mBrandRightsSource::STATUS_NORMAL;

        $sourceRights = BrandRightsSourceService::getAllSource('*', $sWhere);

        $sourceRights = array_column($sourceRights, null, 'id');

        foreach ($levelRights as $key => $item) {
            $rights = $sourceRights[$item['source_rights_id']];
            $levelSourceRights[$key] = $item;
            $levelSourceRights[$key]['rights_name'] = $rights['rights_name'];
            $levelSourceRights[$key]['rights_type'] = $rights['rights_type'];
        }

        return $levelSourceRights;

    }


    public static function getRightsByRightsName($rightsName)
    {
        $rightsInfo = [];
        $sourceRights = BrandRightsSourceService::getOneSource($rightsName);
        if (empty($sourceRights)) {
            return $rightsInfo;
        }
        $rightsId = $sourceRights['id'];
        $aResult = mBrandLevelRightsSource::dataSelectInfo([
            'source_rights_id' => $rightsId,
            'data_status' => mBrandLevelRightsSource::STATUS_NORMAL
        ]);
        if ($aResult && is_array($aResult)) {
            $rightsInfo = $aResult;
        }
        if (!empty($rightsInfo)) {
            $rightsInfo['rights_name'] = $sourceRights['rights_name'];
            $rightsInfo['rights_type'] = $sourceRights['rights_type'];
        }
        return $rightsInfo;
    }


}